package shop.xmall.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import shop.xmall.framework.core.tree.NgTree;
import shop.xmall.system.mapper.SysDeptMapper;
import shop.xmall.system.domain.SysDept;
import shop.xmall.system.service.ISysDeptService;
import shop.xmall.utils.DateUtils;
import shop.xmall.utils.text.Convert;

/**
 * 部门Service业务层处理
 *
 * @author xmall
 * @date 2019-10-14
 */
@Service
public class SysDeptServiceImpl implements ISysDeptService {
    @Autowired
    private SysDeptMapper sysDeptMapper;

    /**
     * 查询部门
     *
     * @param deptId 部门ID
     * @return 部门
     */
    @Override
    public SysDept selectSysDeptById(Long deptId) {
        return sysDeptMapper.selectSysDeptById(deptId);
    }

    /**
     * 查询部门列表
     *
     * @param sysDept 部门
     * @return 部门
     */
    @Override
    public List<SysDept> selectSysDeptList(SysDept sysDept) {
        return sysDeptMapper.selectSysDeptList(sysDept);
    }

    /**
     * 新增部门
     *
     * @param sysDept 部门
     * @return 结果
     */
    @Override
    public int insertSysDept(SysDept sysDept) {
        sysDept.setCreateTime(DateUtils.getNowDate());
        if (sysDept.getParentId() == null || sysDept.getParentId() == 0) {
            sysDept.setAncestors("0");
        } else {
            SysDept p = selectSysDeptById(sysDept.getParentId());
            sysDept.setAncestors(p.getAncestors() + "," + p.getDeptId());
        }
        return sysDeptMapper.insertSysDept(sysDept);
    }

    /**
     * 修改部门
     *
     * @param sysDept 部门
     * @return 结果
     */
    @Override
    public int updateSysDept(SysDept sysDept) {
        sysDept.setUpdateTime(DateUtils.getNowDate());
        return sysDeptMapper.updateSysDept(sysDept);
    }

    /**
     * 删除部门对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteSysDeptByIds(String ids) {
        return sysDeptMapper.deleteSysDeptByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除部门信息
     *
     * @param deptId 部门ID
     * @return 结果
     */
    public int deleteSysDeptById(Long deptId) {
        return sysDeptMapper.deleteSysDeptById(deptId);
    }

    /**
     * 部门列表信息转化为ngTree结构
     *
     * @param list
     * @return
     */
    @Override
    public List<NgTree<SysDept>> convertTreeToList(List<SysDept> list) {
        NgTree<SysDept> root = null;
        List<NgTree<SysDept>> parents = new ArrayList<>();
        Map<Long, List<NgTree<SysDept>>> childrenMap = new HashMap<>();
        for (SysDept sysDept : list) {
            if (0 == sysDept.getParentId()) {
                root = convertNgTree(sysDept);
                root.setLevel(0);
                root.setExpanded(true);
                parents.add(root);
                continue;
            }
            List<NgTree<SysDept>> ngTrees = childrenMap.get(sysDept.getParentId());
            if (ngTrees == null) {
                ngTrees = new ArrayList<>();
            }
            ngTrees.add(convertNgTree(sysDept));
            childrenMap.put(sysDept.getParentId(), ngTrees);
        }
        for (NgTree<SysDept> parent : parents) {
            buildTree(parent, childrenMap);
        }
        return parents;
    }

    private NgTree<SysDept> convertNgTree(SysDept sysDept) {
        NgTree<SysDept> ngTree = new NgTree<SysDept>();
        ngTree.setKey(sysDept.getDeptId());
        ngTree.setTitle(sysDept.getDeptName());
        ngTree.setItem(sysDept);
        return ngTree;
    }

    private void buildTree(NgTree<SysDept> parent, Map<Long, List<NgTree<SysDept>>> childrenMap) {
        if (childrenMap.containsKey(parent.getKey())) {
            List<NgTree> children = new ArrayList<>();
            List<NgTree<SysDept>> list = childrenMap.get(parent.getKey());
            if (list != null) {
                for (NgTree<SysDept> child : list) {
                    child.setLevel(parent.getLevel() + 1);
                    child.setExpanded(true);
                    buildTree(child, childrenMap);
                    children.add(child);
                }
            }
            parent.setChildren(children);
        } else {
            parent.setExpanded(true);
            parent.setIsLeaf(true);
        }
    }
}
